package cn.tedu.db.service.impl;

import cn.tedu.db.dao.UserDao;
import cn.tedu.db.entity.User;
import cn.tedu.db.exception.*;
import cn.tedu.db.service.AwardService;
import cn.tedu.db.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    AwardService awardService;

    UserDao userDao;

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.READ_COMMITTED)
    public User regist(String username,String password) {
        if (username==null || password==null) {
            throw new IllegalParameterException("参数异常！");
        }

        User findUser = userDao.findUserByName(username);
        if (findUser!=null) {
            throw new UserNameIsUsedException("该用户名已经被注册！");
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        int i = userDao.addUser(user); //执行insert user ....
        System.out.println(i);
        if (i!=1) {
            throw new RegistFaildException("注册用户失败！");
        }
        //授予奖励积分 执行了 update award
        awardService.grantAward(user, "注册", 100);
        return user;
    }

    @Override
    @Transactional
    public User login(String username, String password) {
        if (StringUtils.hasText(username) || StringUtils.hasText(password)) {
            throw new IllegalParameterException("用户名密码不能为空！");
        }
        User user = userDao.findUserByName(username);

        if (user==null) {
            throw new UserNotFoundException("该用户不存在！");
        }
        if (!user.getPassword().equals(password)) {
            throw new PasswordErrorException("密码错误！");
        }
        awardService.grantAward(user, "登陆", 10);
        return user;
    }

    @Override
    @Transactional(readOnly = true)
    public List<User> list() {
        return userDao.findAllUser();
    }

    @Override
    @Transactional(readOnly = true)
    public User getById(Integer id) {
        return userDao.findUserById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public User getByUsername(String username) {
        return userDao.findUserByName(username);
    }

    @Override
    @Transactional
    public void update(User user) {
        int n = userDao.updateUser(user);
        if (n != 1){
            throw new UpdateFaildException("更新失败！");
        }
    }

    @Override
    @Transactional
    public void delete(User user) {
        int n = userDao.deleteUser(user.getId());
        if (n != 1){
            throw new DeleteFaildException("更新失败！");
        }
    }
}
